package com.kws.merchant.service.impl;


import com.kws.merchant.domain.OperationRepertoryType;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.mapper.OperationRepertoryTypeMapper;
import com.kws.merchant.service.OperationRepertoryTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;

/**
 * 运维-库存管理备件类型(OperationRepertoryType)表服务实现类
 *
 * @author yxy
 * @since 2024-10-28 15:39:22
 */
@Service("operationRepertoryTypeService")
public class OperationRepertoryTypeServiceImpl extends ServiceImpl<OperationRepertoryTypeMapper, OperationRepertoryType> implements OperationRepertoryTypeService {

    @Resource
    private OperationRepertoryTypeMapper operationRepertoryTypeMapper;


    /**
     * 分页查询列表
     */
    @Override
    public Page<OperationRepertoryType> pageList(OperationRepertoryType operationRepertoryType) {
        Page<OperationRepertoryType> page = new Page<>(0, 10);
        LambdaQueryWrapper<OperationRepertoryType> queryWrapper = Wrappers.lambdaQuery();

        return this.page(page, queryWrapper);
    }

    @Override
    public List<OperationRepertoryType> listByPid(Long pid) {
        return this.list(Wrappers.<OperationRepertoryType>lambdaQuery().eq(OperationRepertoryType::getPid,pid));
    }

    @Override
    public  List<OperationRepertoryType> repertoryTypeList() {
        List<OperationRepertoryType> list = this.listByPid(0l);
        list.forEach(x->{
            List<OperationRepertoryType> treeList=getTreeList(x.getId());
            x.setChildrenList(treeList);
        });
        return list;
    }

    /**
     * 递归获取子节点数据
     * @param id
     * @return
     */
    private List<OperationRepertoryType> getTreeList(Long id) {
        List<OperationRepertoryType> operationRepertoryTypes = this.listByPid(id);
        operationRepertoryTypes.forEach(x->{
            List<OperationRepertoryType> treeList = getTreeList(x.getId());
            x.setChildrenList(treeList);
        });
        return operationRepertoryTypes;
    }


}
